package com.renwei.yunlong.global;

import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.Spanned;

import com.renwei.yunlong.bean.FriendEnterpriseBean;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.contacts.Contact;
import com.renwei.yunlong.event.ContactCheckEvent;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.greenrobot.event.EventBus;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.global
 * Created by :  Benjamin.
 * Created time: 2017/11/23 10:28
 * Changed by :  Benjamin.
 * Changed time: 2017/11/23 10:28
 * Class description:
 */

public class AppHelper {
    private static ArrayList<Contact> checkContactList = new ArrayList<>();
    private static ArrayList<Contact> groupList = new ArrayList<>();
    private static HashMap<String, Contact> checkMap = new LinkedHashMap<>();
    private static HashMap<String, Contact> groupMap = new LinkedHashMap<>();//群成员已有列表
    private static FriendEnterpriseBean myCompanyInfo = new FriendEnterpriseBean();

    public static FriendEnterpriseBean getMyCompanyInfo() {
        return myCompanyInfo;
    }

    public static void setMyCompanyInfo(FriendEnterpriseBean myCompanyInfo) {
        AppHelper.myCompanyInfo = myCompanyInfo;
    }

    public static void setCheck(Contact contact) {
        String employeeId = contact.getEmployeeId();
        if (checkMap.get(employeeId) == null) {
            checkMap.put(employeeId, contact);
        } else {
            checkMap.remove(employeeId);
        }
        EventBus.getDefault().post(new ContactCheckEvent(""));
    }


    /**
     * 根据联系人手机号获取名称
     *
     * @param mobile
     * @return
     */
    public static String getContactNameByNum(String mobile) {
        String tempMobile = StringUtils.value(mobile).trim();
        Contact contact = checkMap.get(tempMobile);
        String name = "";
        if (contact == null) {
            name = "未知用户";
        } else {
            name = "".equals(StringUtils.value(contact.getName())) ? StringUtils.value(contact.getEmployeeName()) : StringUtils.value(contact.getName());
        }
        return name;
    }


    /**
     * 将群成员添加到 map集合中保证 后面已经有的群成员不可选中的状态
     *
     * @param contact 该成员的对象
     */
    public static void setGroupMember(Contact contact) {
        String employeeId = contact.getEmployeeId();
        if (groupMap.get(employeeId) == null) {
            groupMap.put(employeeId, contact);
        } else {
            groupMap.remove(employeeId);
        }
    }
    /**
     * 删除
     *
     * @param employeeId 该成员的对象id
     */
    public static void deleteGroupMember(String employeeId) {
        if (groupMap.get(employeeId) != null) {
            groupMap.remove(employeeId);
        }
    }

    /**
     * 判断该成员是否是群组已经有的成员了
     *
     * @param contact
     * @return
     */
    public static boolean hasGroupMember(Contact contact) {
        String employeeId = contact.getEmployeeId();
        Contact tempContact = groupMap.get(employeeId);
        return tempContact != null;
    }

    // TODO: 2017/12/14 获取选中联系人列表
    public static ArrayList<Contact> getCheckContactList() {
        checkContactList.clear();
        for (String s : checkMap.keySet()) {
            checkContactList.add(checkMap.get(s));
        }
        return checkContactList;
    }


    // TODO: 2017/12/14 获取群组列表
    public static ArrayList<Contact> getGropList() {
        groupList.clear();
        for (String s : checkMap.keySet()) {
            groupList.add(checkMap.get(s));
        }
        return groupList;
    }


    /**
     * 根据手机号来判断当前条目是否已经选中 且不为已有群成员
     *
     * @param contact
     * @return
     */
    public static boolean hasContact(Contact contact) {
        String employeeId = contact.getEmployeeId();
        if (checkMap.get(employeeId) == null) {
            return false;
        } else {
            return true;
        }
    }


    //创建群组成功之后清空创建的列表
    public static void clearCheckList() {
        checkContactList.clear();
        checkMap.clear();
        groupMap.clear();
    }


    /**
     * 判断是否为管理员或者服务台
     *
     * @param userBean
     * @return
     */
    public static boolean isAdminOrServerDesk(Object userBean) {
        if (userBean instanceof LoginBean) {//判断业主是否为管理员
            return ((LoginBean) userBean).getRows() != null && ((LoginBean) userBean).getRows().getEmployee()
                    != null && ((LoginBean) userBean).getRows().getEmployee().getRoleId()
                    != null && "12".equals(((LoginBean) userBean).getRows().getEmployee().getRoleId()) || "10".equals(((LoginBean) userBean).getRows().getEmployee().getRoleId());
        } else if ((userBean instanceof ServiceLoginBean))//判断服务上是否为管理员
        {
            return ((ServiceLoginBean) userBean).getRows() != null && ((ServiceLoginBean) userBean).getRows().getEmployee()
                    != null && ((ServiceLoginBean) userBean).getRows().getEmployee().getRoleId()
                    != null && "12".equals(((ServiceLoginBean) userBean).getRows().getEmployee().getRoleId()) || "10".equals(((ServiceLoginBean) userBean).getRows().getEmployee().getRoleId());
        } else {
            return false;
        }
    }

    /**
     * 判断是否为管理员
     *
     * @param userBean
     * @return
     */
    public static boolean isAdminDesk(Object userBean) {
        if (userBean instanceof LoginBean) {//判断业主是否为管理员
            return ((LoginBean) userBean).getRows() != null && ((LoginBean) userBean).getRows().getEmployee()
                    != null && ((LoginBean) userBean).getRows().getEmployee().getRoleId()
                    != null && "12".equals(((LoginBean) userBean).getRows().getEmployee().getRoleId());
        } else if ((userBean instanceof ServiceLoginBean))//判断服务上是否为管理员
        {
            return ((ServiceLoginBean) userBean).getRows() != null && ((ServiceLoginBean) userBean).getRows().getEmployee()
                    != null && ((ServiceLoginBean) userBean).getRows().getEmployee().getRoleId()
                    != null && "12".equals(((ServiceLoginBean) userBean).getRows().getEmployee().getRoleId());
        } else {
            return false;
        }
    }

    public static void openFile(String filrUrl) {
        List<String> imagePattern = new ArrayList<>();
        imagePattern.add("jpg");
        imagePattern.add("png");
        imagePattern.add("gif");
        File file = new File(filrUrl);
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        LogUtil.e("文件格式" + suffix);
    }


    public static InputFilter emojiFilter = new InputFilter() {

        Pattern emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            Matcher emojiMatcher = emoji.matcher(source);
//            if (emojiMatcher.find()) {
            for (int index = start; index < end; index++) {
                int type = Character.getType(source.charAt(index));
                if (type == Character.SURROGATE) {
                    return "";
                } else {
                    if (emojiMatcher.find()) {
                        return "";
                    }
                }
            }
            return null;
        }
    };

   public static RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
       int mScrollThreshold;

       @Override
       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
           super.onScrolled(recyclerView, dx, dy);
           boolean isSignificantDelta = Math.abs(dy) > mScrollThreshold;
           if (isSignificantDelta) {
               if (dy > 0) {
                   onScrollUp();
               } else {
                   onScrollDown();
               }
           }
       }

       /**
        * 下滑监听
        */
       private void onScrollDown() {

       }

       /**
        * 上滑监听
        */
       private void onScrollUp() {

       }
   };

}
